package com.guigu.servicebase.exceptionhandler;

import com.guigu.commonutils.R;
import com.guigu.commonutils.ResultCode;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Author: janecraft
 * @Date: 2024/9/9
 * @Description: com.guigu.systemservice.controller
 * @Version： 1.0
 */
@ControllerAdvice
@Controller
public class GlobalExceptionHandler {

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public R handleArithmeticException(ArithmeticException ex) {
        System.out.println(ex.getMessage());
       return R.error().message("除数不能为0").code(ResultCode.ERROR);
    }

    @ExceptionHandler(NumberFormatException.class)
    @ResponseBody
    public R handleNumberFormatException(NumberFormatException ex) {
        System.out.println(ex.getMessage());
        return R.error().message("格式转换出错").code(ResultCode.ERROR);
    }

    @ExceptionHandler(MyCustomeException.class)
    @ResponseBody
    public R handleMyCustomeException(MyCustomeException ex) {
        System.out.println(ex.getMessage());
        return R.error().message(ex.getMsg()).code(ex.getCode());
    }


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public R handleException(Exception ex) {
        System.out.println(ex.getMessage());
        return R.error().message("未知错误").code(ResultCode.ERROR);
    }

}
